//
//  VKSettingController.m
//  V-Learn
//
//  Created by liubo on 2019/1/10.
//  Copyright © 2019 yxk. All rights reserved.
//

#import "VKSettingController.h"
#import "VKSettingModel.h"
#import "VKSettingCell.h"
#import "VKUpdatePassController.h"
#import "VKAboutAppController.h"
#import "WYHXFileTool.h"
#import "LBLoginVC.h"
#define cachePath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]

@interface VKSettingController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation VKSettingController
-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, LBNAVBAR_HEIGHT, ScreenW, ScreenH - LBNAVBAR_HEIGHT) style:UITableViewStyleGrouped];
        _tableView.tableFooterView = [UIView new];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 70;
        
        _tableView.backgroundColor = [UIColor cz_colorWithRed:244 green:244 blue:244];
        [self setupHeaderView];
    }
    return _tableView;
}
- (void)setupHeaderView{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 12)];
    view.backgroundColor = [UIColor cz_colorWithRed:244 green:244 blue:244];
    self.tableView.tableHeaderView = view;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
//    self.edgesForExtendedLayout = @[];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;
    }

    self.title = @"设置";
    [self tableView];
    [self.view addSubview:_tableView];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.01;
    }
    return 0.01;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == 0) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen cz_screenWidth], [UIScreen cz_screenHeight] - LBNAVBAR_HEIGHT - 222)];
        view.backgroundColor = [UIColor cz_colorWithRed:244 green:244 blue:244];
        UIButton *commitBtn = [[UIButton alloc]init];
        [view addSubview:commitBtn];
        [commitBtn setImage:[UIImage imageNamed:@"vkbtn"] forState:UIControlStateNormal];
        [commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(view).offset(-130);
            make.left.equalTo(@15);
            make.width.equalTo(@([UIScreen cz_screenWidth] - 30));
            make.height.equalTo(@55);
        }];
        commitBtn.imageView.contentMode = UIViewContentModeScaleToFill;
        [commitBtn addTarget:self action:@selector(commitBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
        UILabel *commitLabel = [[UILabel alloc]init];
        [view addSubview:commitLabel];
        commitLabel.textColor = [UIColor whiteColor];
        commitLabel.font = [UIFont systemFontOfSize:16];
        commitLabel.text = @"退出登录";
        [commitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(commitBtn).offset(15);
            make.centerX.equalTo(commitBtn);
        }];
        return view;
    }
    
    return nil;
}

- (void)commitBtnClick{
    //退出
    [UserModel clearUserInfo];
//    NSLog(@"%@",[UserModel getUserModel].token);
//    [self.navigationController popViewControllerAnimated:YES];
    
//    UIWindow *window = [UIApplication sharedApplication].keyWindow;
//    window.rootViewController = [[LBLoginVC alloc]init];
//            NSString *userMoney = [[NSUserDefaults standardUserDefaults] objectForKey:@"userMoney"];
    [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"userMoney"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return [UIScreen cz_screenHeight] - LBNAVBAR_HEIGHT - 222;
    }
    return 0.01;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    VKSettingCell *cell = [VKSettingCell cellWithTableView:tableView];
    VKSettingModel *model = [[VKSettingModel alloc]init];
    if (indexPath.row == 0) {
        model.content = @"修改密码";
        model.row = 0;
        model.rightStr = @"";
        cell.model = model;
    }else if (indexPath.row == 1){
        model.content = @"关于APP";
        model.row = 1;
        model.rightStr = @"";
        cell.model = model;
    }else if (indexPath.row == 2){
        model.content = @"清除缓存";
        model.row = 2;
        [WYHXFileTool getFileSize:cachePath completion:^(NSInteger totalSize) {
            NSString *rightStr = [self cacheSizeStr:totalSize];
            if ([rightStr isEqualToString:@""]) {
                model.rightStr = @"0M";
            }else{
                model.rightStr = rightStr;
            }
            cell.model = model;

        }];
        
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) { // 修改密码
        [self.navigationController pushViewController:[[VKUpdatePassController alloc]init] animated:YES];
    }else if (indexPath.row == 1){ // 关于APP
        [self.navigationController pushViewController:[[VKAboutAppController alloc]init] animated:YES];
    }else if (indexPath.row == 2){
        [WYHXFileTool removeDirectoryPath:cachePath];
        [WYHXFileTool getFileSize:cachePath completion:^(NSInteger totalSize) {
            [MBProgressHUD showSuccess:@"清理成功" toView:self.view];
            [self.tableView reloadData];
        }];
    }
}
- (NSString *)cacheSizeStr:(NSInteger)totalSize {
    NSString *sizeStr = @"";
    if (totalSize > 1000 * 1000) {
        CGFloat sizeF = totalSize / 1000.0 / 1000.0;
        sizeStr = [NSString stringWithFormat:@"%@%.1fMB", sizeStr, sizeF];
    } else if (totalSize > 1000) {
        CGFloat sizeF = totalSize / 1000.0;
        sizeStr = [NSString stringWithFormat:@"%@%.1fKB", sizeStr, sizeF];
    } else if (totalSize > 0) {
        sizeStr = [NSString stringWithFormat:@"%@%.ldB", sizeStr, totalSize];
    }
    return sizeStr;
}
@end
